package handler

import (
	"cargo-list/cache"
	"cargo-list/models"
	"encoding/json"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func GoodDetail(c *gin.Context) {
	id := c.Query("id")
	var good models.GoodOrderList
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("goods").Select("good_orders.id AS OrderId",
			"goods.`name` AS Name",
			"goods.num AS Num",
			"goods.price AS Price",
			"good_orders.order_code AS OrderCode",
			"good_orders.order_num AS OrderNum",
			"good_orders.order_price AS OrderPrice",
		).
			Joins("LEFT JOIN good_orders ON goods.id=good_orders.id").
			Where("goods.id=?", id).Find(&good)
		jsonData, _ := json.Marshal(good)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &good)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": good,
	})
	return
}
func GoodList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var good []models.GoodOrderList
	//SELECT
	//good_orders.id AS OrderId,
	//goods.`name` AS Name,
	//goods.num AS Num,
	//goods.price AS Price ,
	//good_orders.order_code AS OrderCode,
	//good_orders.order_num AS OrderNum,
	//good_orders.order_price AS OrderPrice
	//FROM
	//goods
	//LEFT JOIN good_orders ON goods.id=good_orders.id
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("goods").Select("good_orders.id AS OrderId",
			"goods.`name` AS Name",
			"goods.num AS Num",
			"goods.price AS Price",
			"good_orders.order_code AS OrderCode",
			"good_orders.order_num AS OrderNum",
			"good_orders.order_price AS OrderPrice",
		).
			Joins("LEFT JOIN good_orders ON goods.id=good_orders.id").
			Offset(offset).Limit(size).Find(&good)
		jsonData, _ := json.Marshal(good)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &good)
	}
	var total int64
	models.DB.Model(&models.GoodOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  good,
		"total": total,
	})
	return
}
func GoodOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_price := c.PostForm("order_price")
	order_num := c.PostForm("order_num")
	data := models.GoodOrder{
		Model:      gorm.Model{},
		OrderCode:  order_code,
		OrderPrice: order_price,
		OrderNum:   order_num,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func GoodAdd(c *gin.Context) {
	name := c.PostForm("name")
	price := c.PostForm("price")
	num := c.PostForm("num")
	data := models.Goods{
		Model: gorm.Model{},
		Name:  name,
		Price: price,
		Num:   num,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
